package com.thinkgem.jeesite.common.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.context.support.XmlWebApplicationContext;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.ins.entity.InsTable;
import com.thinkgem.jeesite.modules.ins.entity.InsTableUser;
import com.thinkgem.jeesite.modules.ins.service.InsTableUserService;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 检验报告书过滤器
 * @author fanqinglong
 *
 */
@Component
public class InsFilter implements Filter {
	private InsTableUserService insTableUserService;

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {

	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;
		String ope = req.getParameter("ope");
		String tableId = "";
		tableId = request.getParameter("tableId");
		if (StringUtils.isBlank(tableId)) {
			tableId = request.getParameter("table.id");
		}
		if (StringUtils.isBlank(tableId)) {
			tableId = request.getParameter("id");
		}
		String newTable = request.getParameter("newTable");
		ServletContext sc = request.getSession().getServletContext();
		XmlWebApplicationContext cxt = (XmlWebApplicationContext)WebApplicationContextUtils.getWebApplicationContext(sc);
		if(cxt != null && cxt.getBean("insTableUserService") != null && insTableUserService == null){
			insTableUserService = (InsTableUserService) cxt.getBean("insTableUserService");
		}
		User user = UserUtils.getUser();
		InsTableUser param = new InsTableUser();
		param.setUser(user);
		param.setTable(new InsTable(tableId));
		InsTableUser resultTableUser = insTableUserService.getByTableAndUser(param);
		String url = request.getRequestURI();
		boolean flag = true;
		//数据修改
		if (url.indexOf("insData/fill")>0 && "update".equals(ope) && (null == resultTableUser || (resultTableUser.getCode() & 128) != 128)) {
			flag = false;
		}
		//导出
		if (url.indexOf("insData/exportList")>0 && (null == resultTableUser || (resultTableUser.getCode() & 64) != 64)) {
			flag = false;
		}
		//导入
		if (url.indexOf("insData/toImport")>0 && (null == resultTableUser || (resultTableUser.getCode() & 32) != 32)) {
			flag = false;
		}
		//打印
		if (url.indexOf("insData/print")>0 && (null == resultTableUser || (resultTableUser.getCode() & 16) != 16)) {
			flag = false;
		}
		//表修改
		if (!"newTable".equals(newTable) && url.indexOf("insTable/form")>0 && (null == resultTableUser || (resultTableUser.getCode() & 8) != 8)) {
			flag = false;
		}
		//表删除
		if (url.indexOf("insTable/delete")>0 && (null == resultTableUser || (resultTableUser.getCode() & 4) != 4)) {
			flag = false;
		}
		//绑定模板
		if ((url.indexOf("insTemplate/bindList")>0 || url.indexOf("unbindTemplate")>0) && (null == resultTableUser || (resultTableUser.getCode() & 2) != 2)) {
			flag = false;
		}
		//数据填报
		if (url.indexOf("insData/fill")>0 && "insert".equals(ope) && (null == resultTableUser || (resultTableUser.getCode() & 1) != 1)) {
			flag = false;
		}
		if (flag) {
			chain.doFilter(req, resp);
		}else {
			String dispatchUrl = Global.getAdminPath()+"/sys/noAuth/view";
			request.getRequestDispatcher(dispatchUrl).forward(request, response);
			//response.sendRedirect(Global.getAdminPath()+"/sys/noAuth/view");
		}
	}

	@Override
	public void destroy() {
	}

}
